package org.example.smartbuy.controller;

import cn.hutool.core.util.ObjectUtil;
import org.example.smartbuy.Result;
import org.example.smartbuy.common.enums.ResultCodeEnum;
import org.example.smartbuy.common.enums.RoleEnum;
import org.example.smartbuy.entity.Users;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.smartbuy.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Tag(name = "User Controller", description = "Operations related to user management")
public class UserController {

    @Autowired
    private IUsersService userService;

    /**
     * 获取所有用户
     * @return List<User>
     */
    @GetMapping("/list")
    @Operation(summary = "Get all users", description = "Fetch a list of all users",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result listAllUsers() {
        return Result.success(userService.listAll());
    }

    /**
     * 根据ID查询单个用户
     * @param id 用户ID
     * @return User
     */
    @GetMapping("/{id}")
    @Operation(summary = "Get a user by ID", description = "Fetch a user by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result getUserById(@PathVariable int id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 添加新用户
     * @param user 用户对象
     * @return User
     */
    @PostMapping("/register")
    @Operation(summary = "Register a new user", description = "Create a new user",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result addUser(@RequestBody Users user) {
        return Result.success(userService.add(user));
    }

    /**
     * 更新用户信息
     * @param user 更新的用户信息
     * @return User
     */
    @PutMapping("/update")
    @Operation(summary = "Update a user", description = "Update an existing user",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result updateUser(@RequestBody Users user) {
        return Result.success(userService.update(user));
    }

    /**
     * 删除用户
     * @param id 用户ID
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "Delete a user", description = "Delete a user by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result deleteUser(@PathVariable int id) {
        userService.delete(id);
        return  Result.success();
    }

    /*
    * 登录
    * */
    @PostMapping("/login")
    public Result login(@RequestBody Users users) {
        if (ObjectUtil.isEmpty(users.getUsername()) || ObjectUtil.isEmpty(users.getPassword())
                || ObjectUtil.isEmpty(users.getRole())) {
            return Result.fail(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.USER.name().equals(users.getRole())) {
            users = userService.login(users);
        } //家else if zuoshenfenpanduan
        else {
            return Result.fail(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        return Result.success(users);
    }


}
